<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/database.php";
class Bankverbindung {
    
    public static $TABLENAME = "bankverbindung";
    public static $CLASSNAME = "Bankverbindung";
    
    private $id = "";
    private $name = "";
    private $blz = "";
    private $kontonummer = "";
    private $einzugsermaechtigung = FALSE;
    
    public function __construct($id="") {
        if($id!="" && $id!=0) {
            $temp = Database::getDefault()->openId(Bankverbindung::$TABLENAME,$id);
            foreach($temp as $key => $value) {
                $this->$key = $value;
            }
        }
    }
    
	public function getJsonData() {
		$var = get_object_vars($this);
		foreach($var as &$value) {
			if(is_object($value) && method_exists($value, 'getJsonData')) {
				$value = $value->getJsonData();
            } else {
                $value = utf8_encode($value);
			}
		}
		return $var;
	}
	
	static public function search($value) {
	    global $VEREIN;
		$query = "SELECT DISTINCT * FROM " . $VEREIN['prefix'] . Bankverbindung::$TABLENAME . " WHERE name LIKE '%".$value."%' ORDER BY name";
		return Database::getDefault()->execute($query,Bankverbindung::$CLASSNAME);
	}
	
	public function getLabel() {
		return $this->getName()."\nBLZ: ".$this->getBlz()."\nKontonr.: ".$this->getKontonummer();
	}
	
    public function getId() {
        return $this->id;
    }
    
    public function setName($value) {
        $this->name = $value;
    }
    
    public function getName() {
        return $this->name;
    }
    
    public function setBlz($value) {
        $this->blz = $value;
    }
    
    public function getBlz() {
        return $this->blz;
    }
    
    public function setKontonummer($value) {
        $this->kontonummer = $value;
    }
    
    public function getKontonummer() {
        return $this->kontonummer;
    }
    
    public function setEinzugsermaechtigung($value) {
        $this->einzugsermaechtigung = $value;
    }
    
    public function isEinzugsermaechtigung() {
        return $this->einzugsermaechtigung;
    }

    public function delete() {
        Database::getDefault()->delete("bankverbindung",$this->id);
    }
    
    public function save() {
        $values = array(
            'name' => $this->name,
            'blz' => $this->blz,
            'kontonummer' => $this->kontonummer,
            'einzugsermaechtigung' => $this->einzugsermaechtigung
        );
        if($this->id=="") {
            $this->id = Database::getDefault()->insert(Bankverbindung::$TABLENAME,$values);
        } else {
            Database::getDefault()->update(Bankverbindung::$TABLENAME,$values,$this->id);
        }
    }
    
    static public function getAll() {
        global $VEREIN;
        $query = "SELECT * FROM " . $VEREIN['prefix'] . Bankverbindung::$TABLENAME . " ORDER BY name";
        return Database::getDefault()->execute($query, Bankverbindung::$CLASSNAME);
    }
    
    /**
     * create table for banking contacts or update if something to do
     */
    public static function initDatabase() {
        global $VEREIN;
        echo "<b>update / create " . Bankverbindung::$TABLENAME . "</b><br />";
        $query = "CREATE TABLE IF NOT EXISTS " . $VEREIN['prefix'] . Bankverbindung::$TABLENAME . " (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            die(Database::getDefault()->getConnection()->error);
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Bankverbindung::$TABLENAME . " ADD COLUMN name VARCHAR(200) NOT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Bankverbindung::$TABLENAME . " ADD COLUMN blz VARCHAR(8) NOT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Bankverbindung::$TABLENAME . " ADD COLUMN kontonummer VARCHAR(12) NOT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Bankverbindung::$TABLENAME . " ADD COLUMN einzugsermaechtigung TINYINT(1) NOT NULL DEFAULT 0";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Bankverbindung::$TABLENAME . " ADD COLUMN iban VARCHAR(22) DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Bankverbindung::$TABLENAME . " ADD COLUMN bic VARCHAR(12) DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Bankverbindung::$TABLENAME . " ADD COLUMN mandat VARCHAR(20) DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Bankverbindung::$TABLENAME . " ADD COLUMN adress_id INT DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        echo "<br />";
    }
}
?>